package top.lixunda.ecommerce.server.user.feign;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.business.api.entity.dto.PageDTO;
import top.lixunda.ecommerce.server.user.api.entity.db.BusinessTaskDO;
import top.lixunda.ecommerce.server.user.api.entity.dto.BusinessTaskCloseDTO;
import top.lixunda.ecommerce.server.user.api.entity.dto.BusinessTaskCommitDTO;
import top.lixunda.ecommerce.server.user.api.entity.dto.BusinessTaskQueryDTO;
import top.lixunda.ecommerce.server.user.api.entity.dto.BusinessTaskStartDTO;
import top.lixunda.ecommerce.server.user.api.entity.vo.BusinessTaskVO;
import top.lixunda.ecommerce.server.user.api.feign.IFeignBusinessTaskController;
import top.lixunda.ecommerce.server.user.service.IBusinessTaskService;

import java.util.List;

/**
 * 业务代办任务管理接口
 *
 * @author lixunda
 * @version time:2020/4/13 13:56
 */
@Slf4j
@RestController
public class FeignBusinessTaskController extends BaseController
        implements IFeignBusinessTaskController {

    private final IBusinessTaskService businessTaskService;

    @Autowired
    public FeignBusinessTaskController(IBusinessTaskService businessTaskService) {
        this.businessTaskService = businessTaskService;
    }

    /**
     * 通过taskId查询当前登陆人员的代办任务信息，登陆权限
     *
     * @param taskId 任务编号
     * @return 代办任务信息，只包括当前登陆人员的任务分配数据
     */
    @Override
    public MessageObject<BusinessTaskVO> queryCurrentUserTaskByTaskId(Integer taskId) {
        currentLoginUserCacheNotNull();
        BusinessTaskVO result = businessTaskService.queryCurrentUserTaskByTaskId(taskId);
        return ObjectValidators.isEmpty(result) ? getErrorMessageObject("查询数据失败")
                : getSuccessMessageObject("查询数据成功", result);
    }

    /**
     * 通过taskId查询当前登陆人员的代办任务信息，登陆权限
     *
     * @param taskName 任务名称
     * @return 代办任务信息，只包括当前登陆人员的任务分配数据
     */
    @Override
    public MessageObject<List<BusinessTaskVO>> queryCurrentUserByTaskName(String taskName) {
        currentLoginUserCacheNotNull();
        List<BusinessTaskVO> result = businessTaskService.queryCurrentUserByTaskName(taskName);
        return getSuccessMessageObject("查询数据成功", result);
    }

    /**
     * 通过条件分页查询查询当前登陆人员的代办任务信息，登陆权限
     *
     * @param pageDTO 条件分页数据
     * @return 分页的代办任务信息
     */
    @Override
    public MessageObject<Page<BusinessTaskVO>> queryCurrentUserTaskByPageWrapper(
            PageDTO<BusinessTaskDO, BusinessTaskQueryDTO> pageDTO) {
        currentLoginUserCacheNotNull();
        IPage<BusinessTaskVO> result = businessTaskService.queryCurrentUserTaskByPageWrapper(pageDTO);
        return getSuccessMessageObject("查询数据成功", (Page<BusinessTaskVO>) result);
    }

    /**
     * 开启任务，内部调用权限
     *
     * @param list 开启任务参数
     * @return 开启的流程
     */
    @Override
    public MessageObject<List<BusinessTaskVO>> startTask(List<BusinessTaskStartDTO> list) {
        List<BusinessTaskVO> result = businessTaskService.startTask(list);
        return getSuccessMessageObject("开启任务成功", result);
    }

    /**
     * 提交任务，内部调用权限
     *
     * @param list 提交任务参数
     * @return 开启的流程
     */
    @Override
    public MessageObject<List<BusinessTaskVO>> commitTask(List<BusinessTaskCommitDTO> list) {
        List<BusinessTaskVO> result = businessTaskService.commitTask(list);
        return getSuccessMessageObject("提交任务成功", result);
    }

    /**
     * 关闭任务，内部调用权限
     *
     * @param list 关闭流程参数
     * @return 是否关闭成功
     */
    @Override
    public MessageObject<Boolean> closeTask(List<BusinessTaskCloseDTO> list) {
        Boolean result = businessTaskService.closeTask(list);
        return ObjectValidators.isEmpty(result) || !result ?
                getErrorMessageObject("关闭任务失败") :
                getSuccessMessageObject("提交任务成功", true);
    }

    /**
     * 根据taskId删除任务，内部调用，登陆权限
     *
     * @param taskIds 任务编号列表
     * @return 关闭的任务数据
     */
    @Override
    public MessageObject<List<BusinessTaskVO>> removeTaskByTaskIds(List<Integer> taskIds) {
        currentLoginUserCacheNotNull();
        List<BusinessTaskVO> result = businessTaskService.removeTaskByTaskIds(taskIds);
        return getSuccessMessageObject("删除任务成功", result);
    }
}
